package com.tjsinfo.tjsoa.system.action;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.jdbc.core.RowMapper;

import com.tjsinfo.tjsoa.Dao.ISpringDao;
import com.tjsinfo.tjsoa.common.ToolKit;
import com.tjsinfo.tjsoa.documentationflow.xingzhengfawen.vo.MainType;
import com.tjsinfo.tjsoa.documentationflow.xingzhengfawen.vo.SubTypeInfo;

/**
 * 发文管理
 * @author Administrator
 *
 */
public class TemplateSelectAction extends DispatchAction{
	private ISpringDao springDao;
	
	public ISpringDao getSpringDao() {
		return springDao;
	}

	public void setSpringDao(ISpringDao springDao) {
		this.springDao = springDao;
	}

	public ActionForward getMaintypeList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		List<MainType> maintypeList =  selectMainType();
		JSONArray array = new JSONArray();
		for (MainType mainTypeInfo : maintypeList) {
			JSONObject	 object = new JSONObject(mainTypeInfo);
			array.put(object);
		}
		response.getWriter().print(array);
		return null;
		
	}
	
	public ActionForward getSubtypeList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Integer maintypeid = Integer.valueOf(request.getParameter("mainTypeId"));
		List<SubTypeInfo> subTypeList = selectSubType(maintypeid);
		JSONArray array = new JSONArray();
		for (SubTypeInfo subTypeInfo : subTypeList) {
			JSONObject	 object = new JSONObject(subTypeInfo);
			array.put(object);
		}
		response.getWriter().print(array);
		return null;
		
	}
	
	
	/**
	 *  查询一级类型
	 * @param nosql 
	 * @return
	 */
	public List<MainType> selectMainType(){
		StringBuffer sql = new StringBuffer("SELECT * FROM dbo.t_system_maintype");
		List<MainType> list = new ArrayList<MainType>();
		list = springDao.querySQL(sql.toString(), new Object[]{}, new RowMapper(){

			public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				MainType mainType = new MainType();
				mainType.setId(arg0.getInt("id"));
				mainType.setMainType(arg0.getString("maintype"));
				return mainType;
			}
			
		});
		
		return list;
	}
	
	/**
	 *  查询子类型
	 * @param nosql 
	 * @return
	 */
	public List<SubTypeInfo> selectSubType(Integer mainTypeId){
		StringBuffer sql = new StringBuffer("SELECT * FROM dbo.t_system_doctype WHERE maintypeId = ?");
		List<SubTypeInfo> list = new ArrayList<SubTypeInfo>();
		list = springDao.querySQL(sql.toString(), new Object[]{mainTypeId}, new RowMapper(){

			public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				SubTypeInfo subTypeInfo = new SubTypeInfo();
				subTypeInfo.setId(arg0.getInt("id"));
				subTypeInfo.setTempList(arg0.getInt("templist"));
				subTypeInfo.setTempName(arg0.getString("tempname"));
				subTypeInfo.setTypeName(arg0.getString("typename"));
				return subTypeInfo;
			}
			
		});
		
		return list;
	}	

}
